<?php
declare(strict_types = 1);

namespace app\common\model\mysql;

use think\Model;

class User extends Model
{
    protected $name = 'mall_user';

    protected $pk = 'id';

    //用户状态
    const STATUS_NORMAL = 1;
    const STATUS_PEDDING = 0;
    const STATUS_DELETE = 99;

    static $statusArr = [
        self::STATUS_NORMAL => '正常',
        self::STATUS_PEDDING => '待审核',
        self::STATUS_DELETE => '删除',
    ];

    public function findUserByPhone($phone)
    {
        if (empty($phone)) {
            return false;
        }

        $where = [
            'phone_number' => trim($phone),
        ];

        return $this->where($where)->find();
    }

    public function updateUserById($id, $data)
    {
        $id = intval($id);
        if (empty($id) || empty($data) || !is_array($data)) {
            return false;
        }

        $where = [
            'id' => $id,
        ];
        return $this->where($where)->save($data);
    }

    public function getUserById($id)
    {
        $id = intval($id);
        if (empty($id)) {
            return false;
        }

        return $this->find($id);
    }

    public function getUserByUsername($username)
    {
        if (empty($username)) {
            return false;
        }

        $where = [
            'username' => trim($username),
        ];

        return $this->where($where)->find();
    }

    public function updateById($id, $data)
    {
        $id = intval($id);
        if(empty($id) || empty($data) || !is_array($data)) {
            return false;
        }
        $where = [
            'id' => $id,
        ];
        return $this->where($where)->save($data);
    }

}
